package jwdt.xadmin.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jwdt.common.dao.BaseDao;
import jwdt.common.util.NewObjectUtil;
import jwdt.xadmin.system.bean.QXJS;
import jwdt.xadmin.system.service.QXManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.StringUtil;

@SuppressWarnings({"rawtypes","unchecked"})
public class QXManageServiceImpl implements QXManageService{
	
	private BaseDao<QXJS> baseDao;
	
	public void setBaseDao(BaseDao<QXJS> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doList(PageUtil pu) throws Exception{
		String sql = " order by jsdm";
		pu.doList(QXJS.class,sql);
		//创造json数据对象
		return pu;
	}
	@Override
	public String doAddact(QXJS obj) throws Exception {
		return baseDao.doAddObj(obj);
	}

	@Override
	public String doEditact(QXJS obj, String jsdm) throws Exception {
		QXJS obj1 = (QXJS)Jdb.get().get(QXJS.class, jsdm);
		String ret = "修改成功";
		if(obj1 == null) {
			ret = "您要修改的记录不存在jsdm="+jsdm;
			return ret;
		}
		try {
			Jdb.get().update(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改失败";
		}
		return ret;
	};
	
	@Override
	public String doDelact(String jsdm) throws Exception {
		QXJS obj = (QXJS)Jdb.get().get(QXJS.class, jsdm);
		String ret = "删除成功";
		if(obj == null) {
			ret = "您要删除的记录不存在：JSDM=" + jsdm;
			return ret;
		}
		try {
			Jdb.get().execute("delete from jwdt_qx_jscd where jsdm='" + jsdm + "'");
			Jdb.get().execute("delete from jwdt_qx_jscdmx where jsdm='"+jsdm+"'");
			Jdb.get().del(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除成功";
		}
		return ret;
	}

	@Override
	public QXJS doLoad(String jsdm) throws Exception {
		QXJS obj;
		if(jsdm!=null && !"".equals(jsdm)){
			obj = (QXJS)Jdb.get().get(QXJS.class, jsdm);
		}else{
			obj = (QXJS)NewObjectUtil.getNewObject(QXJS.class);
		}
		return obj;
	}
	
	public String doListJsForTree(String oskey,String flag) throws Exception {
		String sql = "";
		String sql1 = "";
		List<Map<String, String>> list2 = null;
		if(!StringUtil.nullOrBlank(flag)){
			sql = "SELECT a.cddm, a.cdmc FROM jwdt_qx_cd a,jwdt_qx_jscd b WHERE a.CDDM=b.CDDM AND b.JSDM='"+oskey+"' AND a.zt=1 AND a.sjcd='' ORDER BY a.px";
		}else{
			sql = "SELECT a.cddm, a.cdmc FROM jwdt_qx_cd a WHERE a.zt=1 AND a.sjcd='' ORDER BY a.px";
			sql1 = "SELECT a.cddm, a.cdmc FROM jwdt_qx_cd a,jwdt_qx_jscd b WHERE a.CDDM=b.CDDM AND b.JSDM='"+oskey+"' AND a.zt=1 AND a.sjcd='' ORDER BY a.px";
			list2 = Jdb.get().query(sql1);
		}
		List<Map<String, String>> list = Jdb.get().query(sql);
		StringBuffer tree = new StringBuffer("[");
		for (Map<String, String> map : list) {
			tree.append("{\"id\":\"" + map.get("cddm")+"\"");
			tree.append(",\"text\":\"" + map.get("cdmc") + "\"");
			tree.append(",\"attributes\":{ \"jscd\":true}");
			String children = listCdForTree(oskey,map.get("cddm"),flag);
			if(!"[]".equals(children)){
				tree.append(",\"state\":\"closed\"");
				tree.append(",\"children\":").append(children);
			}else{
				if(list2!=null){
					if(list2.contains(map)){
						tree.append(",\"checked\":true");
					}
				}
			}
			tree.append("},");
		}
		if(list.size()>0){
			tree.deleteCharAt(tree.length()-1);
		}
		tree.append("]");
		//生成json字符串
    	return tree.toString();
	}
	
	public String listCdForTree(String oskey,String sjcd,String flag){
		String sql = "";
		String sql1 = "";
		List<Map<String, String>> list1=null;
		if(!StringUtil.nullOrBlank(flag)){
			sql = "SELECT a.cddm, a.cdmc,a.mxqx FROM jwdt_qx_cd a,jwdt_qx_jscd b WHERE a.CDDM=b.CDDM AND b.JSDM='"+oskey+"' AND zt=1 AND sjcd='"+sjcd+"' ORDER BY sjcd,px";
		}else{
			sql = "SELECT a.cddm, a.cdmc,a.mxqx FROM jwdt_qx_cd a where a.zt = 1 and sjcd='"+sjcd+"' ORDER BY sjcd,px";
			sql1 = "SELECT a.cddm, a.cdmc,a.mxqx FROM jwdt_qx_cd a,jwdt_qx_jscd b WHERE a.CDDM=b.CDDM AND b.JSDM='"+oskey+"' AND zt=1 AND sjcd='"+sjcd+"' ORDER BY sjcd,px";
			list1 = Jdb.get().query(sql1);
		}
		List<Map<String, String>> list = Jdb.get().query(sql);
		StringBuffer tree = new StringBuffer("[");
		for (Map<String, String> map : list) {
			tree.append("{\"id\":\"" + map.get("cddm")+"\"");
			tree.append(",\"text\":\"" + map.get("cdmc") + "\"");
			tree.append(",\"attributes\":{ \"jscd\":true}");
			if("1".equals(map.get("mxqx"))){
				String children = listLmForTree(oskey,null,"0",flag);
				if(!"[]".equals(children)){
					tree.append(",\"state\":\"closed\"");
					tree.append(",\"children\":").append(children);
				}else{
					if(list1!=null){
						if(list1.contains(map)){
							tree.append(",\"checked\":true");
						}
					}
				}
			}else{
				if(list1!=null){
					if(list1.contains(map)){
						tree.append(",\"checked\":true");
					}
				}
			}
			tree.append("},");
		}
		if(list.size()>0){
			tree.deleteCharAt(tree.length()-1);
		}
		tree.append("]");
		return tree.toString();
	}
	
	public String listLmForTree(String oskey,List<Map<String, String>> list,String fid,String flag){
		List<Map<String, String>> list1 = null;
		if(!StringUtil.nullOrBlank(flag)){
			list = Jdb.get().query("select id,name,fid from t_type where bview=1 and fid = "+fid+" and id in (SELECT mxdm FROM jwdt_qx_jscdmx WHERE jsdm = '"+oskey+"')");
		}else{
			list = Jdb.get().query("select id,name,fid from t_type where bview=1 and fid = "+fid);
		}
		if(!StringUtil.nullOrBlank(oskey)){
			list1 = Jdb.get().query("select id,name,fid from t_type where bview=1 and fid = "+fid+" and id in (SELECT mxdm FROM jwdt_qx_jscdmx WHERE jsdm = '"+oskey+"')");
		}
		StringBuffer tree = new StringBuffer("[");
		for (Map<String, String> map : list) {
			tree.append("{\"id\":\"" + map.get("id")+"\"");
			tree.append(",\"text\":\"" + map.get("name") + "\"");
			List<Map<String, String>> newlist = Jdb.get().query("select id,name,fid from t_type where bview=1 and fid = " + map.get("id"));
			if(newlist.size()>0){
				tree.append(",\"state\":\"closed\"");
				tree.append(",\"children\":").append(listLmForTree(oskey,newlist,map.get("id"),flag));
			}else{
				if(list1 != null){
					if(list1.contains(map) && newlist.size()==0){
						tree.append(",\"checked\":true");
					}
				}
			}
			tree.append("},");
		}
		if(list.size()>0){
			tree.deleteCharAt(tree.length()-1);
		}
		tree.append("]");
		return tree.toString();
	}
	
	public String doSetRoleAct(String jsdm,String cddm,String fldm,String jscds,String idss) throws Exception {
		String[] jscd = jscds.split(",");
		String[] ids = idss.split(",");
		QXJS qxjs = (QXJS)Jdb.get().get(QXJS.class, jsdm);
		String ret = "操作成功";
		if(qxjs == null) {
			ret = "您要设置的记录不存在：ID="+jsdm;
			return ret;
		}
		try {
			Jdb.get().execute("delete from jwdt_qx_jscd where jsdm='" + jsdm + "'");
			List sqls = new ArrayList(jscd.length);
			for(int i=0; i<jscd.length; i++) {
				sqls.add("insert into jwdt_qx_jscd(jsdm,cddm) values('" + jsdm + "','" + jscd[i] + "')");
			}
			Jdb.get().executeBatch(sqls);
			Jdb.get().execute("delete from jwdt_qx_jscdmx where cddm='"+cddm+"' and jsdm='"+jsdm+"' and fldm='jscd'");
			String sql = "insert into jwdt_qx_jscdmx(cddm, jsdm, fldm, mxdm) values('"+cddm+"', '"+jsdm+"', '"+fldm+"', ?)";
			for(int i=0; i<ids.length; i++) {
				Jdb.get().execute(sql, new Object[]{ids[i]});
			}
		} catch (Exception e) {
			ret = "操作出错";
		}
		return ret;
	}
	
	@Override
	public String doUpdateState(String jsdm, String result) throws Exception {
		return baseDao.doIntUpdateState(QXJS.class, "jsdm='"+jsdm+"'", result);
	}
}
